// miniprogram/pages/wait/list.js
import * as R from 'ramda';
import U from '../../utils/index';
import config from '../../config/index'

const getTabDatas = taskTypeNames => {
  const typeTasks = taskTypeNames.map((item, index) => ({
    name: item,
    param: {
      task_type: index + 1,
      is_complete: 0
    },
  }));
  return R.concat(
    R.concat(
      [{
        name: '全部',
        param: { is_complete: 0 },
      }],
      typeTasks,
    ), [{
      name: '已完成',
      param: { is_complete: 1},
    }]
  )
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taskTypeNames: config.taskTypeNames,
    tabDatas: getTabDatas(config.taskTypeNames),
    index: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ index: options.index });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    R.compose(
      () => this.turnPage(this.data.index),
      () => this.data.tabDatas.map((value, index) => {
        this.setData({
          [ 'currentPageData.pageData' + index ]: [],
          [ 'currentPageData.pageNum' + index ]: 1,
          [ 'currentPageData.isLastPage' + index ]: false,
          [ 'currentPageData.isShowPageLoading' + index ]: false,
        })
      })
    )();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.turnPage(this.data.index);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  onClickTab(e) {
    R.compose(
      R.when(
        () => this.data.currentPageData['pageNum' + this.data.index] === 1,
        () => this.turnPage(this.data.index)
      ),
      () => this.setData({ index: e.detail.index }),
      () => wx.pageScrollTo({
        scrollTop: 0,
        duration: 0,
      })
    )();
  },
  turnPage(index) {
    U.turnPage(() => U.reqPost({
      url:'/task/task_list',
      data: {
        ...this.data.tabDatas[index].param,
        page_num: this.data.currentPageData['pageNum' + index],
        page_size: config.defaultPageSize
      }
    }), {
      pageData: 'pageData' + index,
      pageNum: 'pageNum' + index,
      isLastPage: 'isLastPage' + index,
      isShowPageLoading: 'isShowPageLoading' + index,
    }).run(this);
    return;
  }
})
